package com.alex.util.maths;

import java.text.NumberFormat;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author alex
 */
public class MathUtil {
    /**
     * 获取随机数
     *
     * @param strLength
     * @return
     */
    public static String random(int strLength) {
        Random rm = new Random();
        // 获得随机数
        double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);
        // 将获得的获得随机数转化为字符串
        String fixLenthString = String.valueOf(pross);
        // 返回固定的长度的随机数
        String randomStr = fixLenthString.substring(2, strLength + 2);
        return randomStr;
    }

    /**
     * 获取1到10随机数
     */
    public static int randomOneToTen() {
        // 定义随机类
        Random random = new Random();
        // 返回[0,10)集合中的整数，注意不包括10
        int result = random.nextInt(10);
        // +1后，[0,10)集合变为[1,11)集合，满足要求
        return result + 1;
    }

    public static String randomNumberAndChart(int roandLength, int chartLength) {
        String result = "";
        if (roandLength <= 0 || chartLength <= 0) {
            return result;
        }
        if (chartLength > roandLength) {
            chartLength = roandLength;
        }
        StringBuilder sb = new StringBuilder();
        // 随机用以下三个随机生成器
        Random rand = new Random();
        Random randdata = new Random();
        int data = 0;
        for (int i = 0; i < roandLength; i++) {
            int index = rand.nextInt(chartLength);
            // 目的是随机选择生成数字，大小写字母
            switch (index) {
                case 0:
                    // 仅仅会生成0~9
                    data = randdata.nextInt(10);
                    sb.append(data);
                    break;
                case 1:
                    // 保证只会产生65~90之间的整数
                    data = randdata.nextInt(26) + 65;
                    sb.append((char) data);
                    break;
                case 2:
                    // 保证只会产生97~122之间的整数
                    data = randdata.nextInt(26) + 97;
                    sb.append((char) data);
                    break;
                default:
                    // 仅仅会生成0~9
                    data = randdata.nextInt(10);
                    sb.append(data);
            }
        }
        result = sb.toString().toLowerCase();
        return result;
    }

    public static String computePercent(int child, int parent) {
        if (parent <= 0) {
            return "-";
        }

        //计算回款率  催回笔数/（新增分案数+变更案件数）
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float) child / (float) (parent) * 100) + "%";
        return result;
    }

    public static String computePercentDouble(double child, double parent) {
        if (parent <= 0) {
            return "-";
        }

        //计算回款率  催回笔数/（新增分案数+变更案件数）
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format(child / (parent) * 100) + "%";
        return result;
    }

    /**
     * 验证是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        boolean bo = false;
        try {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(str);
            if (isNum.matches()) {
                bo = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bo;
    }
}
